//json
function main(tbl){
	if(req.contentType!=null&&req.contentType.indexOf("json")!= -1){
		try {
			tbl=attr("tbl")
		} catch (e) {
			print(e)
		}
	}
	let u=attr("u")
	let conf=u.conf;
	let Vins=Java.type("jk.Vins")
	if(isEmpty(tbl.vin)||!Vins.isVin(tbl.vin)){
		$update({".table":"app_buy","cxnum":u.cxnum+1,"id":u.bid})
		let tab=new Record();
		tab.put(".table","app_log")
		tab.put("username",u.username)
		tab.put("ms",1)
		tab.put("uid",u.id)
		tab.put("type",u.type)
		tab.put("ip",ip())
		tab.put("reqres","请求失败")
		tab.put("resmsg","VIN参数错误")
		tab.put("param",tbl.vin)
		$save(tab)
		return {code:1105,msg:"VIN参数错误"}
	}
	let time1=System.currentTimeMillis();
	let vin=tbl.vin.toUpperCase();
	let car=dao.fetch("app_car",Cnd.where("vin","like",vin.substring(0,8)+"_"+vin.substring(9,10)+"%"))
	let res="{}";
	let lang="en";
	if(!isEmpty(tbl.lang)){
		lang=tbl.lang;
	}
	let reqres="请求成功"
	let resmsg="查询成功"
	if(car!=null){
		res= {"Result":car.result,
			"Info":car.info,
			"Additional":car.additional
		}
	}else{
		let xmlInput = `<root><appkey>37864f8296a34751</appkey>
			<appsecret>437242c1516a434a84160d33a0a85d23</appsecret>
			<method>level.vehicle.vin.get</method>
			<requestformat>json</requestformat>
			<vin>${vin}</vin></root>`;
			  xmlInput = xmlInput.replaceAll("<", "&lt;");
			  xmlInput = xmlInput.replaceAll(">", "&gt;");
			  let soap = `<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body> <LevelData xmlns="http://www.dat881.com/"><xmlInput>${xmlInput}</xmlInput></LevelData>  </soap12:Body></soap12:Envelope>`
			let head=Header.create();
			head.set("Content-Type", "application/soap+xml;charset=utf-8");
			let str=Http.post3("http://service.vin114.net/req", soap,head, 60000).getContent();
			str=str.substring(str.indexOf("{"),str.lastIndexOf("}")+1);
			let data=foJson(str)
			if(!isEmpty(data.Result)){//成功
				let tab=new Record();
				tab.put(".table","app_car")
				tab.put("vin",vin)
				tab.put("result",data.Result)
				tab.put("info",data.Info)
				tab.put("additional",data.Additional)
				dao.insert(tab)
				res= {"Result":data.Result,
					"Info":data.Info,
					"Additional":data.Additional
				}
			}else{
				resmsg="查无此车"
				res= {"code":404,"msg":resmsg}
			}
	}
	let res1=baozhuang(res,lang,vin,conf);
	let time2=System.currentTimeMillis();
	let time3=(time2-time1)+1;
	let tab=new Record();
	tab.put(".table","app_log")
	tab.put("username",u.username)
	tab.put("ms",time3)
	tab.put("uid",u.id)
	tab.put("type",u.type)
	tab.put("ip",ip())
	tab.put("reqres",reqres)
	tab.put("resmsg",resmsg)
	tab.put("param",tbl.vin)
	$save(tab)
	if(resmsg=="查询成功"){
		$update({".table":"app_buy","cxnum":u.cxnum+1,"id":u.bid})
	}
	return res1;
}

function baozhuang(res,lang,vin,conf){
	let data={}
	let dicts=cache.get("dicts");
	if(dicts==null){
		dicts=dao.select("select name,val from app_dict","dict")
//		dicts.put("VinYear","VIN年份")
		cache.put("dicts",dicts);
	}
	let VinYear=vinYear(vin.charAt(9))
	if(!isEmpty(res.Result)){
		let result=res.Result;
		let res1=new ArrayList();
			for(var i=0;i<result.length;i++){
				delete result[i].LevelId
				result[i]["VinYear"]=VinYear;
				if(conf!=null){
					let result1=new JseMap();
					for (let item in result[i]) {
						if(conf.contains(item)){
							result1.put(item,result[i][item])
						}
					}
					result[i]=result1;
				}
				if(lang=="cn"){
					let resmp1=new JseMap();
				for(var item in result[i]){
						if(dicts.containsKey(item)){
							resmp1.put(dicts.get(item),result[i][item])
						}else{
							resmp1.put(item,result[i][item])
						}
				}
				
					res1.add(resmp1);
				}else if(lang=="dev"){
					let resmp1=new JseMap();
					for(var item in result[i]){
						if(dicts.containsKey(item)){
							resmp1.put(item,dicts.get(item))
						}else{
							resmp1.put(item,result[i][item])
						}
					}
					resmp1.put("VinYear","VIN年份")
					res1.add(resmp1);
				}
			}
			
			data["code"]=200;
			if(lang=="cn"){
				data["data"]=res1;
			}else if(lang=="dev"){
				data["data"]=res1;
			}else{
				data["data"]=result;
			}
	}else{
		data["code"]=404;
		data["msg"]="未查找到数据";
	}
	return data;
}

function vinYear(num10){
	let cale = Calendar.getInstance();
    let year=cale.get(Calendar.YEAR)-1980;
    let viny= new Array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","R","S"
    		,"T","V","W","X","Y","1","2","3","4","5","6","7","8","9");
    let years=new JseMap();
    for (let i = 0; i <=year; i++) {
    	let j=i;
    	if(j>29) {
    		j=j-30;
    	}
    	years.put(viny[j], i+1980);
	}
	return years.get(num10+"")
}